﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Lswweb.Domain.Payment.Paypal
{
    public class Paypal : IPayment
    {
        #region Fields and Structure Methods

        private string business;
        private string charset;
        private string itemName;
        private string itemNumber;
        private string amount;
        private string returnURL;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        private string cancelURL;
        private string notifyURL;
        private string custom;
        private string currencyCode;

        public bool enableSandbox;

        public Paypal(string business, string itemName, string amount)
            : this(business,itemName,amount,false) { }

        public Paypal(string business, string itemName, string amount, bool enableSandbox)
            : this(business, itemName, string.Empty, amount, "utf-8", string.Empty, string.Empty, string.Empty, string.Empty, "USD", enableSandbox) { }

        public Paypal(string business,string itemName, string itemNumber, string amount,string charset, string returnURL, string cancelURL, string notifyURL, string custom, string currencyCode, bool enableSandbox)
        {
            if (string.IsNullOrEmpty(business))
                throw new ArgumentNullException("business");

            if (string.IsNullOrEmpty(itemName))
                throw new ArgumentNullException("itemNumber");

            if (string.IsNullOrEmpty(amount))
                throw new ArgumentNullException("amount");

            this.business = business;
            this.itemName = itemName;
            this.itemNumber = itemNumber;
            this.amount = amount;
            this.charset = charset;
            this.returnURL = returnURL;
            this.cancelURL = cancelURL;
            this.notifyURL = notifyURL;
            this.custom = custom;
            this.currencyCode = currencyCode;
            this.Note = PaypalOptionState.Close;
            this.Shipping = PaypalOptionState.Close;
            this.enableSandbox = enableSandbox;
        }

        #endregion

        #region Properties

        /// <summary>
        /// 付款金额
        /// </summary>
        public string Amount { get { return this.amount; } }

        /// <summary>
        /// 商家的PayPal账户
        /// </summary>
        public string Business { get { return this.business; } }

        /// <summary>
        /// 编码方式
        /// 默认utf-8
        /// </summary>
        public string Charset 
        { 
            get { return this.charset; }
            set { this.charset = value; }
        }

        /// <summary>
        /// 取消付款之后转向的URL
        /// </summary>
        public string CancelURL 
        { 
            get { return this.cancelURL; }
            set { this.cancelURL = value; }
        }

        /// <summary>
        /// 自定义付递信息（不会显示给客户）
        /// </summary>
        public string Custom 
        { 
            get { return this.custom; }
            set { this.custom = value; }
        }

        /// <summary>
        /// 付款货币符号
        /// 默认为美元（US）
        /// </summary>
        public string CurrencyCode 
        {
            get { return this.currencyCode; }
            set { this.currencyCode = value; }
        }

        /// <summary>
        /// 商品名
        /// </summary>
        public string ItemName { get { return this.itemName; } }

        /// <summary>
        /// 商品编号
        /// </summary>
        public string ItemNumber 
        { 
            get { return this.itemNumber; }
            set { this.itemNumber = value; }
        }

        /// <summary>
        /// 仅与 IPN 一起使用。发送 IPN Form Post 的互联网 URL
        /// </summary>
        public string NotifyURL 
        { 
            get { return this.notifyURL; }
            set { this.notifyURL = value; }
        }

        /// <summary>
        /// 付款之后转向的URL
        /// </summary>
        public string ReturnURL 
        { 
            get { return this.returnURL; }
            set { this.returnURL = value; }
        }

        /// <summary>
        /// Paypal付款界面右上角出现的商家Logo图片 大小为 150 x 50
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// Paypal付款界面是否提示用户输入付款说明
        /// </summary>
        public PaypalOptionState Note { get; set; }

        /// <summary>
        /// Paypal付款界面是否提示用户输入收货地址
        /// </summary>
        public PaypalOptionState Shipping { get; set; }
        

        #endregion

        public void SendPost(HttpContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            string formHTML = this.BuilderService().BuilderFormHTML();

            context.Response.Write(formHTML);
        }

        private PaymentService BuilderService()
        {
            var paymentGateway = this.enableSandbox
                ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
                : "https://www.paypal.com/cgi-bin/webscr";

            PaymentService service = new PaymentService(paymentGateway);

            service.PaymentParameters.Add("cmd", "_xclick");
            service.PaymentParameters.Add("business", this.Business);
            service.PaymentParameters.Add("charset", this.Charset);
            service.PaymentParameters.Add("item_name", this.ItemName);
            service.PaymentParameters.Add("item_number", this.ItemNumber);
            service.PaymentParameters.Add("amount", this.Amount);
            service.PaymentParameters.Add("return", this.ReturnURL);
            service.PaymentParameters.Add("cancel_return", this.CancelURL);
            service.PaymentParameters.Add("notify_url", this.NotifyURL);
            service.PaymentParameters.Add("custom", this.Custom);
            service.PaymentParameters.Add("currency_code", this.CurrencyCode);

            //Paypal付款界面是否提示用户输入收货地址
            //0为显示，1为不显示
            service.PaymentParameters.Add("no_shipping","1");
            
            //Paypal付款界面是否提示用户输入付款说明
            //0为显示，1为不显示
            service.PaymentParameters.Add("no_note","1"); 

            return service;
        }
    }

    public enum PaypalOptionState
    {
        /// <summary>
        /// 关闭
        /// </summary>
        Close=0,

        /// <summary>
        /// 打开 
        /// </summary>
        Open=1
    }
}
